<extend name="Index:base" />
<block name="body">
<div class="hd-menu-list">
    <ul>
        <li class="active"><a href="#">备份数据</a></li>
        <li><a href="{:U('index',array('type'=>'import'))}">还原数据</a></li>
    </ul>
</div>
<form action="{:U('export')}" method="post" id="export-form" autocomplete="off">
	<table class="hd-table hd-table-list hd-form" id="table_list">
		<thead>
		<tr>
		   <td class="hd-w50">
		       <label><input id="select_all" type="checkbox"  checked=""/> 全选</label>
		   </td>
		   <td>表名</td>
		   <td>数据量</td>
		   <td>数据大小</td>
		   <td>创建时间</td>
		   <td>备份状态</td>
		   <td>操作</td>
		</tr>
		</thead>
		<tbody>
			<volist name="list" id="table">
            <tr>
                <td class="num">
                    <input class="ids" checked="chedked" type="checkbox" name="tables[]" value="{$table.name}">
                </td>
                <td>{$table.name}</td>
                <td>{$table.rows}</td>
                <td>{$table.data_length|format_bytes}</td>
                <td>{$table.create_time}</td>
                <td class="info">未备份</td>
                <td class="action">
                    <a href="javascript:;" onclick="hd_ajax('{:U('optimize',array('tables'=>$table['name']))}','','__SELF__')">优化表</a>&nbsp;
                    <a href="javascript:;" onclick="hd_ajax('{:U('repair',array('tables'=>$table['name']))}','','__SELF__')">修复表</a>
                </td>
            </tr>
        </volist>
		</tbody>
	</table>
	<a href="javascript:;" class="hd-btn" id="export">立即备份</a>
	<a id="optimize" class="hd-btn" href="{:U('optimize')}">优化表</a>
  <a id="repair" class="hd-btn" href="{:U('repair')}">修复表</a>
</form>


<script type="text/javascript">
(function($){
  var $form = $("#export-form"), $export = $("#export"), tables
      $optimize = $("#optimize"), $repair = $("#repair");

  $optimize.add($repair).click(function(){
      $.post(this.href, $form.serialize(), function(data){
          if(data.status){
            // alert(data.info,'alert-success');
              hd_alert({message: data.info});
              return true;
          } else {
          	hd_alert({message: data.info});
              return false;
          };
      }, "json");
      return false;
  });

  $export.click(function(){
      $export.parent().children().addClass("disabled");
      // $export.html("正在发送备份请求...");
      $.post(
          $form.attr("action"),
          $form.serialize(),
          function(data){
              if(data.status){
                  tables = data.tables;
                  $export.html(data.info + "开始备份，请不要关闭本页面！");
                  backup(data.tab);
                  window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
              } else {
              	 hd_alert({message: data.info});
                  return false;
                  $export.parent().children().removeClass("disabled");
                  $export.html("立即备份");
              }
          },
          "json"
      );
      return false;
  });

  function backup(tab, status){
      status && showmsg(tab.id, "开始备份...(0%)");
      $.get($form.attr("action"), tab, function(data){
          if(data.status){
              showmsg(tab.id, data.info);

              if(!$.isPlainObject(data.tab)){
                  $export.parent().children().removeClass("disabled");
                  $export.html("备份完成，点击重新备份");
                  window.onbeforeunload = function(){ return null }
                  return;
              }
              backup(data.tab, tab.id != data.tab.id);
          } else {
              hd_alert({message: data.info});
              return false;
              $export.parent().children().removeClass("disabled");
              $export.html("立即备份");
          }
      }, "json");

  }

  function showmsg(id, msg){
      $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
  }
})(jQuery);
$('#select_all').click(function(){
   $('input[type="checkbox"]').prop("checked",this.checked);
});
</script>	
</block>